package com.xiaozhengkeji.bukkit.chunkmanager;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.UUID;


/**
 * 命令行
 */
public class ChunkManagerCommand implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            MessageUtil.server("必须使用玩家身份打开");
            return true;
        }
        Player player = (Player) sender;
        if (args.length == 0) {
            helper(player);
            return true;
        }
        //获取用户当前世界的UID
        UUID worldID = player.getWorld().getUID();
        switch (args[0]) {
            case "add":
                if (ChunkManagerService.MANAGER_WORLD_LIST.contains(worldID)) {
                    MessageUtil.player(player, "当前世界已经存在区块管理器");
                    return true;
                }
                ChunkManagerService.MANAGER_WORLD_LIST.add(worldID);
                MessageUtil.player(player, "添加区块管理器成功");
                break;
            case "delete":
                if (!ChunkManagerService.MANAGER_WORLD_LIST.contains(worldID)) {
                    MessageUtil.player(player, "当前世界不存在区块管理器");
                    return true;
                }
                ChunkManagerService.MANAGER_WORLD_LIST.remove(worldID);
                MessageUtil.player(player, "删除区块管理器成功");
                break;
            case "maxChunk":
                ChunkManagerService.MAX_CHUNK_SIZE = Long.parseLong(args[1]);
                MessageUtil.player(player, "设置区块管理器最大区块数量成功 -> " + ChunkManagerService.MAX_CHUNK_SIZE);
                break;
            case "run":
                ChunkManagerService.runChunkUninstallTask();
                break;
            case "info":
                break;
            default:
                helper(player);
                return true;
        }
        return true;
    }


    public void helper(Player player) {
        MessageUtil.player(player, "ChunkManager 区块管理器程序");
        MessageUtil.player(player, "/ChunkManager add 添加当前世界进入区块管理器程序");
        MessageUtil.player(player, "/ChunkManager delete 删除当前世界的区块管理器");
        MessageUtil.player(player, "/ChunkManager maxChunk [一次性最大卸载区块数量]");
        MessageUtil.player(player, "/ChunkManager run 强制性执行一次区块卸载任务");
        MessageUtil.player(player, "/ChunkManager info 查询目前服务器执行的区块管理器日志");
    }
}
